package com.qdlc.p2p.biz.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.GoodsService;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.exception.ScoreException;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.dao.GoodsDao;
import com.qdlc.p2p.dal.dao.VerifyLogDao;
import com.qdlc.p2p.dal.dto.Goods;
import com.qdlc.p2p.dal.dto.Operator;
import com.qdlc.p2p.dal.dto.VerifyLog;
import com.qdlc.p2p.dal.model.GoodsModel;

@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsDao goodsDao;

    @Resource
    private VerifyLogDao verifyLogDao;
    
    @Override
    public PageDataList<GoodsModel> findPageList(GoodsModel model) {
        return goodsDao.findPageList(model);
    }

    @Override
    public void save(Goods goods) {
    	goods.setStatus(GoodsModel.WAIT_AUDIT);
    	goods.setType(GoodsModel.WAIT_SHELVES);
    	goods.setAddTime(DateUtil.getNow());
        goodsDao.save(goods);
    }

    @Override
    public Goods findById(long id) {
        return goodsDao.find(id);
    }

    @Override
    public void update(Goods goods) {
        goodsDao.update(goods);
    }

    @Override
    public void verify(GoodsModel model) {
        if (model.getStatus() == GoodsModel.NOT_PASS_AUDIT || model.getStatus() == GoodsModel.PASS_AUDIT) {
            Goods item = goodsDao.find(model.getId());
            item.setStatus(model.getStatus());
            item.setAttribute(model.getAttribute());
            item.setDescription(model.getDescription());
            // 审核日志
            VerifyLog log = new VerifyLog(new Operator(model.getOperatorId()),
                 "goodsVerify", model.getId(), 1, model.getStatus(), model.getOperatorRemark());
            verifyLogDao.save(log);
            goodsDao.update(item);
        }
    }

    @Override
    public void shelves(GoodsModel model) {
        Goods item = goodsDao.find(model.getId());
        // 如果现在是上架操作，并且以前是下架类型，并状态为审核通过，则将状态更新为未审核，待上架状态
        if (model.getType() == GoodsModel.UP_SHELVES && item.getType() == GoodsModel.DOWN_SHELVES && 
        		(item.getStatus() == GoodsModel.PASS_AUDIT || !model.isNeedVerify())) {
            
            item.setType(GoodsModel.WAIT_SHELVES);
            item.setStatus(GoodsModel.WAIT_AUDIT);
            item.setShelvesTime(new Date());
            
            // 上下架日志
            VerifyLog log1 = new VerifyLog(new Operator(model.getOperatorId()),
                 "goodsShelves", model.getId(), 1, GoodsModel.WAIT_SHELVES, model.getOperatorRemark());
            verifyLogDao.save(log1);
            
            // 审核日志
            VerifyLog log2 = new VerifyLog(new Operator(model.getOperatorId()),
                 "goodsVerify", model.getId(), 1, GoodsModel.WAIT_AUDIT, model.getOperatorRemark());
            verifyLogDao.save(log2);
        // 如果是上架操作，并且以前为待上架审核通过，则直接跟新类型
        } else if (model.getType() == GoodsModel.UP_SHELVES && item.getType() == GoodsModel.WAIT_SHELVES && 
                (item.getStatus() == GoodsModel.PASS_AUDIT || !model.isNeedVerify())) {
            
            VerifyLog log = new VerifyLog(new Operator(model.getOperatorId()),
                    "goodsShelves", model.getId(), 1, GoodsModel.UP_SHELVES, model.getOperatorRemark());
            verifyLogDao.save(log);
            item.setShelvesTime(new Date());
            item.setType(GoodsModel.UP_SHELVES);
        // 如果是下架操作，并且以前是上架类型，审核通过，则直接更新类型
        } else if (model.getType() == GoodsModel.DOWN_SHELVES && item.getType() == GoodsModel.UP_SHELVES &&
        		(item.getStatus() == GoodsModel.PASS_AUDIT || !model.isNeedVerify())) {
            
            VerifyLog log = new VerifyLog(new Operator(model.getOperatorId()),
                    "goodsShelves", model.getId(), 1, GoodsModel.DOWN_SHELVES, model.getOperatorRemark());
            verifyLogDao.save(log);
            item.setShelvesTime(new Date());
            item.setType(GoodsModel.DOWN_SHELVES);
        }
        item.setUpdateTime(DateUtil.getNow());
        goodsDao.update(item);
    }

	@Override
	public void checkGoodsName(String name) {
		QueryParam param = QueryParam.getInstance();
		param.addParam("name", name.trim());
		List<Goods> list = goodsDao.findByCriteria(param);
		if (list.size() > 0) {
			throw new ScoreException("该商品名称已存在", BusinessException.TYPE_JSON);
		}
	}
}
